package com.hzqy.web.ac;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.service.multirate.MultirateServiceBean;
import com.hzqy.web.vo.CmCatalogContentVo;
import com.hzqy.web.vo.CmCatalogVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmMultirateVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.PmsZTreeVo;

@Controller
@RequestMapping("/")
public class MultirateAction {
protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private MultirateServiceBean multirateServiceBean;
	
	@RequestMapping("/multirate/multirate_list")
	@ResponseBody
	public Object multirateList(HttpSession session, CmMultirateVo cmMultirateVo, int page, int rows) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			cmMultirateVo.setF_multirate_companyid(userVo.getOrgId());
		}
		Map<String,Object> multirateListMap = new HashMap<String, Object>();
		multirateListMap =  multirateServiceBean.selectMultirateList(cmMultirateVo, page, rows);
		return multirateListMap;
	}
	
	@RequestMapping("/multirate/select_multirate")
	@ResponseBody
	public Object selectMultirate(int f_multirate_id) {
		CmMultirateVo cmMultirateVo = multirateServiceBean.selectMultirate(f_multirate_id);
		return cmMultirateVo;
	}
	
	@RequestMapping("/multirate/delete_multirate")
	@ResponseBody
	public Object deleteMultirate(int f_multirate_id) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.deleteMultirate(f_multirate_id);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/insert_multirate")
	@ResponseBody
	public Object insertMultirate(CmMultirateVo cmMultirateVo,String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.insertMultirate(cmMultirateVo,ids);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","新增内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","新增内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/update_multirate")
	@ResponseBody
	public Object updateMultirate(CmMultirateVo cmMultirateVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.updateMultirate(cmMultirateVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","修改内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","修改内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/pack_list")
	@ResponseBody
	public Object selectPackList(HttpSession session, CmContentVo cmContentVo,int bind_status,int f_ccm_multirateid,int page,int rows) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			cmContentVo.setF_cc_companyid(userVo.getOrgId()+"");
		}
		Map<String,Object> packMap = new HashMap<String, Object>();
		if(bind_status ==1) {
			packMap = multirateServiceBean.selectPackContent(cmContentVo, f_ccm_multirateid, page, rows);
		} else if (bind_status ==2) {
			packMap = multirateServiceBean.selectUnPackContent(cmContentVo, f_ccm_multirateid, page, rows);
		}
		
		return packMap;
	}
	
	@RequestMapping("/multirate/delete_pack")
	@ResponseBody
	public Object deletePack(int f_ccm_contentid,int f_ccm_multirateid) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.deletePack(f_ccm_contentid,f_ccm_multirateid);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","解除绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","解除绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/insert_pack")
	@ResponseBody
	public Object insertPack(int f_ccm_contentid,int f_ccm_multirateid) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.insertPack(f_ccm_contentid,f_ccm_multirateid);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","新增绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","新增绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/batch_pack")
	@ResponseBody
	public Object batchPack(int f_ccm_multirateid,String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.batchPack(f_ccm_multirateid,ids);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/batch_unpack")
	@ResponseBody
	public Object batchUnPack(int f_ccm_multirateid,String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.batchUnPack(f_ccm_multirateid,ids);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量解绑成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量解绑失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/multirate_bind")
	@ResponseBody
	public Object selectBindMultirate(CmMultirateVo cmMultirateVo,int bind_status,int f_ccc_catalogid,int page,int rows) {
		Map<String,Object> bindMap = new HashMap<String, Object>();
		if(bind_status ==1) {
			bindMap = multirateServiceBean.selectBindMultirate(cmMultirateVo,f_ccc_catalogid,page,rows);
		} else if (bind_status ==2) {
			bindMap = multirateServiceBean.selectUnBindMultirate(cmMultirateVo,f_ccc_catalogid,page,rows);
		}
		
		return bindMap;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/multirate/multirate_catalogid_excel_list")
	public void MultirateCatalogidExcelList(CmMultirateVo cmMultirateVo,int bind_status,int f_ccc_catalogid, HttpSession session, HttpServletResponse response){
		Map<String,Object> bindMap = new HashMap<String, Object>();
		if(bind_status ==1) {
			bindMap = multirateServiceBean.selectBindMultirate(cmMultirateVo,f_ccc_catalogid,1,1000000);
		} else if (bind_status ==2) {
			bindMap = multirateServiceBean.selectUnBindMultirate(cmMultirateVo,f_ccc_catalogid,1,1000000);
		}
		List<CmMultirateVo> data=(List<CmMultirateVo>)bindMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_multirate_name","打包名称"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_companyname","组织名称"));
		excelHeader.add(new MyEntry<String,String>("f_ccc_viewindex","排序值"));
		ExcelUtil.generateExcel(data, excelHeader, response);
	}
	
	@RequestMapping("/multirate/delete_bind")
	@ResponseBody
	public Object deleteBind(int f_ccc_catalogid,int f_ccc_contentid) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.deleteBind(f_ccc_catalogid,f_ccc_contentid);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","解除绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","解除绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/insert_bind")
	@ResponseBody
	public Object insertBind(int f_ccc_catalogid,int f_ccc_contentid) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.insertBind(f_ccc_catalogid,f_ccc_contentid);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","新增绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","新增绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/batch_bind")
	@ResponseBody
	public Object batchBind(int f_ccc_catalogid,String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.batchBind(f_ccc_catalogid,ids);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/batch_unbind")
	@ResponseBody
	public Object batchUnBind(int f_ccc_catalogid,String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.batchUnBind(f_ccc_catalogid,ids);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量解绑成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量解绑失败!flag="+flag);
		}
		return resultMap;
	}
	
	
	@RequestMapping("/multirate/batch_update_status")
	@ResponseBody
	public Object batchUpdateStatus(int status,String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.batchUpdateStatus(status,ids);
		resultMap.put("resultTitle", "操作结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量操作成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量操作失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/select_contentBindCatalog")
	@ResponseBody
	public Object selectContentBindCatalog(int f_multirate_id) {
		List<CmCatalogVo> cataList = multirateServiceBean.selectContentBindCatalog(f_multirate_id);
		return cataList;
	}
	
	@RequestMapping("/multirate/select_catalogTree")
	@ResponseBody
	public Object selectCatalogTree() {
		List<PmsZTreeVo> treeList = new ArrayList<PmsZTreeVo>();
		treeList = multirateServiceBean.selectCatalogTree();
		return treeList;
	}
	
	@RequestMapping("/multirate/select_catalogBind")
	@ResponseBody
	public Object selectCatalogBind(int f_ccc_contentid) {
		List<CmCatalogContentVo> cccList = multirateServiceBean.selectCatalogBind(f_ccc_contentid);
		return cccList;
	}
	
	@RequestMapping("/multirate/update_catalogBind")
	@ResponseBody
	public Object updateCatalogBind(int f_ccc_contentid,String cataloginStr,String catalogdeStr) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.updateCatalogBind(f_ccc_contentid,cataloginStr,catalogdeStr);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改绑定成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改绑定失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/select_viewindex")
	@ResponseBody
	public Object selectViewIndex(CmCatalogContentVo cmCatalogContentVo) {
		CmCatalogContentVo vo = multirateServiceBean.selectViewIndex(cmCatalogContentVo);
		return vo;
	}
	
	@RequestMapping("/multirate/update_viewindex")
	@ResponseBody
	public Object updateViewIndex(CmCatalogContentVo cmCatalogContentVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = multirateServiceBean.updateViewIndex(cmCatalogContentVo);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改排序值成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改排序值失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/multirate/multirate_excel_list")
	public void multirateExcelList(CmMultirateVo cmMultirateVo, HttpSession session, int page, int rows, HttpServletResponse response){
		Map<String,Object> multirateListMap = new HashMap<String, Object>();
		multirateListMap =  multirateServiceBean.selectMultirateList(cmMultirateVo, 1, 1000000);
		List<CmMultirateVo> data=(List<CmMultirateVo>)multirateListMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_multirate_id","包Id"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_companyname","企业名称"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_number","包编号"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_name","包名称"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_type","包类型"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_score","包评分"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_cdate","创建时间"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_udate","修改时间"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_onTime","上线时间"));
		excelHeader.add(new MyEntry<String,String>("f_multirate_offTime","下线时间"));
		boolean flag=ExcelUtil.generateExcel(data, excelHeader, response);
	}
}
